package com.yu.proxy.dynamic;

import org.assertj.core.internal.cglib.proxy.Enhancer;
import org.assertj.core.internal.cglib.proxy.MethodInterceptor;

import com.yu.proxy.Calculator;
import com.yu.proxy.CalculatorImpl;

/**
 * @author yushijun
 * @date 2018/6/29
 * Cglib 代理
 */
public class CglibDynamicCalculator {

    public static void main(String[] args) {

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Calculator.class);
//        enhancer.setSuperclass(CalculatorImpl.class);
        enhancer.setCallback((MethodInterceptor) (o, method, objects, methodProxy) -> {
            System.out.println("cglib begin");
            Object invoke = methodProxy.invoke(new CalculatorImpl(), objects);
            System.out.println("cglib end");
            return invoke;
        });
        Calculator calculator = (Calculator) enhancer.create();
        int x = calculator.add(1, 3);
        System.out.printf(x + "");
    }

}
